package com.example.neusoft215gdh.androidpro_21.Fragement;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.neusoft215gdh.androidpro_21.Adapter.peiXunAdapter;
import com.example.neusoft215gdh.androidpro_21.JavaBean.BannerView;
import com.example.neusoft215gdh.androidpro_21.JavaBean.PeiXunJiBie;
import com.example.neusoft215gdh.androidpro_21.JavaBean.PeiXunListBean;
import com.example.neusoft215gdh.androidpro_21.Listener.JieBieListener;
import com.example.neusoft215gdh.androidpro_21.Listener.PeiXunListListener;
import com.example.neusoft215gdh.androidpro_21.Model.PeiXunModel;
import com.example.neusoft215gdh.androidpro_21.R;

import java.util.ArrayList;
import java.util.List;

import static com.example.neusoft215gdh.androidpro_21.R.drawable.b;
import static com.example.neusoft215gdh.androidpro_21.R.drawable.c;

public class PeiXunFragement extends Fragment implements PeiXunListListener, View.OnClickListener, JieBieListener {
    /**
     * 轮播代码
     */
    private int[] imgs = {R.drawable.a, b, c, R.drawable.d};
    private List<View> viewList;
    BannerView bannerView;

    private RecyclerView recyclerView;
    List<PeiXunJiBie> list;
    Button rumen;
    Button cainiao;
    Button jinjie;
    String userid;
    String train="train";
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_train, container, false);
        initRecyclerView(view);
//		PeiXunModel model=new PeiXunModel();
//		model.getList("45",1,this);
        viewList = new ArrayList<View>();
        for (int i = 0; i < imgs.length; i++) {
            ImageView image = new ImageView(getContext());
            image.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            //设置显示格式
            image.setScaleType(ImageView.ScaleType.CENTER_CROP);
            image.setImageResource(imgs[i]);
            viewList.add(image);
        }

        bannerView.startLoop(true);
        bannerView.setViewList(viewList);
        PeiXunModel model = new PeiXunModel();
        model.getJieBie(userid,1,train,1,this);
        rumen.setOnClickListener(this);
        cainiao.setOnClickListener(this);
        jinjie.setOnClickListener(this);
        return view;
    }

    public void initRecyclerView(View view) {
        bannerView = (BannerView) view.findViewById(R.id.banner2);
        recyclerView = (RecyclerView) view.findViewById(R.id.perXun_Rec);
        rumen = (Button) view.findViewById(R.id.rumen);
        cainiao = (Button) view.findViewById(R.id.cainiao);
        jinjie = (Button) view.findViewById(R.id.jinjie);
        recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
        recyclerView.setItemAnimator(new DefaultItemAnimator());

    }

    @Override
    public void onResponse(List<PeiXunListBean> listBeen) {
//        list = listBeen;
//        recyclerView.setAdapter(new peiXunAdapter(getActivity(), list));
    }

    @Override
    public void onResponseRumen(List<PeiXunJiBie> listbean) {
      list=listbean;
        recyclerView.setAdapter(new peiXunAdapter(getActivity(),list));
    }

    @Override
    public void onResponseCainiao(List<PeiXunJiBie> listbean) {
    list=listbean;
        recyclerView.setAdapter(new peiXunAdapter(getActivity(),list));
    }

    @Override
    public void onResponseJinJIe(List<PeiXunJiBie> listbean) {
       list=listbean;
        recyclerView.setAdapter(new peiXunAdapter(getActivity(),list));
    }

    @Override
    public void onFail(String msg) {
        Toast.makeText(getActivity(), "" + msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View v) {
        SharedPreferences user=getActivity().getSharedPreferences("Userid",0);
        userid=user.getString("user_id","");
        switch (v.getId()) {
            case R.id.rumen:
                PeiXunModel model = new PeiXunModel();
                model.getJieBie(userid, 1,train,1, this);
                break;
            case R.id.cainiao:
                PeiXunModel mode2 = new PeiXunModel();
                mode2.getJieBie(userid, 1, train, 2,this);
                break;
            case R.id.jinjie:
                PeiXunModel model3=new PeiXunModel();
                model3.getJieBie(userid,1,train,3,this);
                break;
        }


    }
}
